package com.stary.beetlsql.generator.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

/**
 * <p>模板配置</p>
 * @author stary {@link stary1993@qq.com}
 * @since 2019-7-31
 */
public class TemplateConfig {

	private String entity = Constants.TEMPLATE_ENTITY;

	private String mapper = Constants.TEMPLATE_MAPPER;

	private String md = Constants.TEMPLATE_MD;

	private String service = Constants.TEMPLATE_SERVICE;

	private String serviceImpl = Constants.TEMPLATE_SERVICE_IMPL;

	private String controller = Constants.TEMPLATE_CONTROLLER;

	private Map<String, String> extTemplate;

	public String getEntity() {
		return entity;
	}

	public TemplateConfig setEntity(String entity) {
		this.entity = entity;
		return this;
	}

	public String getMapper() {
		return mapper;
	}

	public TemplateConfig setMapper(String mapper) {
		this.mapper = mapper;
		return this;
	}

	public String getMd() {
		return md;
	}

	public TemplateConfig setMd(String md) {
		this.md = md;
		return this;
	}

	public String getService() {
		return service;
	}

	public TemplateConfig setService(String service) {
		this.service = service;
		return this;
	}

	public String getServiceImpl() {
		return serviceImpl;
	}

	public TemplateConfig setServiceImpl(String serviceImpl) {
		this.serviceImpl = serviceImpl;
		return this;
	}

	public String getController() {
		return controller;
	}

	public TemplateConfig setController(String controller) {
		this.controller = controller;
		return this;
	}

	public Map<String, String> getExtTemplate() {
		return extTemplate;
	}

	public TemplateConfig setExtTemplate(Map<String, String> extTemplate) {
		this.extTemplate = extTemplate;
		return this;
	}

	public GroupTemplate getClasspathTemplate() throws IOException {
		ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
		Configuration cfg = Configuration.defaultConfiguration();
		return  new GroupTemplate(resourceLoader, cfg);		
	}

	/**
	 * 初始化模板
	 * @param packageInfo 包配置
	 * @return
	 * @throws IOException
	 */
	public Map<String, Template> initTemplates(PackageConfig packageInfo) throws IOException {
		Map<String, Template> templates = new HashMap<>();
		GroupTemplate gt = this.getClasspathTemplate();		
		templates.put(packageInfo.getEntity(), gt.getTemplate(this.entity)); // entity模板
		templates.put(packageInfo.getMapper(), gt.getTemplate(this.mapper)); // mapper模板
		templates.put(packageInfo.getService(), gt.getTemplate(this.service)); // service模板
		templates.put(packageInfo.getServiceImpl(), gt.getTemplate(this.serviceImpl)); // serviceImpl模板
		templates.put(packageInfo.getController(), gt.getTemplate(this.controller)); // controller模板
		templates.put(packageInfo.getMdPath(), gt.getTemplate(this.md)); // md模板
		if (getExtTemplate() != null) {
			for (Entry<String, String> entry : getExtTemplate().entrySet()) {
				templates.put(entry.getKey(), gt.getTemplate(entry.getValue()));
			}
		}
		return templates;
	}

}
